<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">
    
    <title>${ webpageTitle }</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	
	<script src="rp/scripts/boot.js" type="text/javascript"></script>
	
	<style type="text/css">
	   	
    </style>
	
  </head>
  
  <body class="body-fit">
  	
  	<div class="mini-splitter" style="width: 100%; height: 100%;" borderStyle="border:0;">
	    <div size="240" showCollapseButton="true" minSize="240">
	    	<div class="mini-toolbar" style="padding:5px;border-top:0;border-left:0;border-right:0;">                
	            <span style="padding-left:5px;">名称：</span>
	            <input class="mini-textbox" style="width: 100px;" id="moduleName" onenter="selectModule"/>
	            <a class="mini-button" onclick="selectModule"><i class="fa fa-search fa-lg"></i> 查询</a>                  
	        </div>
	        <div class="mini-fit">
	            <ul id="moduleTree" class="mini-tree" style="width:100%; height: 100%;"
		  			url="rp/system/moduleMng/getModules" resultAsTree="false" expandOnLoad="true"
		            showTreeIcon="true" idField="id" textField="name" parentField="parentId"
		            onnodeclick="treeNodeClick">        
		        </ul>
	        </div>
	    </div>
	    <div showCollapseButton="false">
	    	<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
	    		<table style="width: 100%;">
	    			<tr>
		    			<td style="width:100%;">
		    				<a class="mini-button" onclick="addRow()"><i class="fa fa-plus fa-lg"></i> 新增</a>
		                   	<a class="mini-button" onclick="saveRows()"><i class="fa fa-floppy-o fa-lg"></i> 保存</a>
		                   	<a class="mini-button mini-button-danger" onclick="removeRows()"><i class="fa fa-trash-o fa-lg"></i> 删除</a>
		                   	<a class="mini-button" onclick="showAllResc()">显示所有</a>
		                   	<a class="mini-button" onclick="rebuildRescWarehouse()">
		                   		<i class="fa fa-refresh fa-lg"></i> 重建资源库
		                   	</a>
		    			</td>
		    			<td style="white-space:nowrap;">
		    				<a class="mini-button" plain="true" 
		    					onclick="openHelpWindow('B248C113-9CF2-4ACC-84EF-D8754046CAB7')"><i class="fa fa-question-circle fa-lg"></i> 帮助</a>
		    			</td>
	    			</tr>
	    		</table>               
            	
	        </div>
	        <div class="mini-fit">
	            <div id="rescGrid" class="mini-datagrid" style="width: 100%; height: 100%;" borderStyle="border:0;"
			    	url="rp/system/rescMng/getResources" idField="id"
			    	pageSize="20" allowResize="false"
			    	allowCellEdit="true" allowCellSelect="true" multiSelect="true" 
			        editNextOnEnterKey="true" editNextRowCell="true" allowCellValid="true"
			        showFilterRow="true">
				    <div property="columns">
				        <div type="indexcolumn" headerAlign="center">序号</div>
				        <div type="checkcolumn"></div>
				        <div name="name" field="name" vtype="required" width="60" headerAlign="center">
				        	资源名称
				        	<input property="editor" class="mini-textbox" style="width:100%;"/>
				        	<input id="nameFilter" property="filter" class="mini-textbox" style="width:100%;" emptyText="输入名称搜索"
		                    	onenter="onNameFilterEnter"/>
				        </div>
				        <div field="url" vtype="required" width="170" headerAlign="center">
				        	URL<input property="editor" class="mini-textbox" style="width:100%;"/>
				        </div>
				        <!-- <div field="moduleId" visible="false">
				        	所属模块ID
				        </div> -->  
				        <div field="moduleId" type="comboboxcolumn" displayField="moduleName" width="50" headerAlign="center" renderer="onModuleRenderer">
				        	所属模块
				        	<input property="editor" class="mini-treeselect" 
				        		popupWidth="200"
				        		url="rp/system/moduleMng/getModules"
				        		valueField="id" textField="name" parentField="parentId"
				        		expandOnLoad="true"/>
				        </div>  
				       	<div field="remark" width="100" headerAlign="center" >
				        	备注<input property="editor" class="mini-textarea" style="width:200px;" minWidth="200" minHeight="50"/>
				        </div>
				        <!-- <div type="comboboxcolumn" autoShowPopup="true" 
				        	name="pingtai" field="pingtai" width="40" align="center" headerAlign="center" renderer="onYesOrNoRenderer">
				        	平台资源
			                <input property="editor" class="mini-combobox" style="width:100%;" data="YesOrNo" />                
			            </div> -->
			            <div width="50" headerAlign="center" renderer="onActionRenderer">
				        	操作
				        </div>
				        <div field="createDate" width="50" headerAlign="center" allowSort="true" renderer="onDateRenderer2">
				        	创建日期
				        </div>                
				    </div>
				</div>
	        </div>
	    </div>        
	</div>
  	
  	
	<script type="text/javascript">
		var moduleTree, grid;
		var selectedModuleId = null;
		var selectedModuleName = null;
		
		$(function(){
			mini.parse();
			moduleTree = mini.get("moduleTree");
			grid = mini.get("rescGrid");
			grid.load();
		});
		
		function selectModule(){
			var moduleName = mini.get("moduleName").getValue();
			//moduleTree.load("rp/system/moduleMng/getModules?moduleName="+moduleName);
			rp_ajax("rp/system/moduleMng/selectModules", {name: moduleName}, function(text){
				var data = mini.decode(text);
				moduleTree.loadList(data);
				moduleTree.expandAll();
			});
		}
		
		function treeNodeClick(e){
			var node = e.node;
			selectedModuleId = node.id;
			selectedModuleName = node.name;
			grid.load({moduleId: selectedModuleId});
		}
		
		function onModuleRenderer(e){
			var value = e.cellHtml;
			return value;
		}
		
		function onActionRenderer(e){
			var record = e.record;
			var actions = "<a class='mini-button mini-button-small' onclick='gotoUrl(&apos;"+record.name+"&apos;, &apos;"+record.url+"&apos;)'><i class='fa fa-globe fa-lg'></i> 跳转链接</a>";
			return actions;
		}
		
		function gotoUrl(_resourceName, _url){
			_url = _url.replace("*","");
			var f = _url.charAt(0);
			if(f == "/"){
				_url = _url.substring(1, _url.length);	
			}
			
			mini.confirm("跳转链接可能存在修改数据的危险，请谨慎操作！", "警告：危险操作！！！",
	            function (action) {
	                if(action == "ok"){
	                	window.parent.addMainTab("跳转链接-来自资源："+_resourceName, _url);  
	                }
	            }
	        );
		}
		
		function addRow() {         
            var newRow = { name: "", moduleId: selectedModuleId, moduleName: selectedModuleName, url: "" };
            grid.addRow(newRow, 0);
            grid.beginEditCell(newRow, "name");
        }
		
		function saveRows() {
			grid.validate();
			if (grid.isValid() == false) {
                var error = grid.getCellErrors()[0];
                grid.beginEditCell(error.record, error.column);
                return;
            }
			
            var data = grid.getChanges();
            var json = rp_encode(data);
            grid.loading("保存中，请稍等......");
            rp_ajax("rp/system/rescMng/saveResources", {data: json}, function(text){
            	grid.reload();
            });
        }
		
		function removeRows(){
			var rows = grid.getSelecteds();
			if(rows.length == 0){
				mini.alert("请勾选需要删除的记录...");
			}else{
				mini.confirm("确定删除记录吗？", "友情提示",
		            function (action) {
		                if (action == "ok") {
		                    var json = rp_encode(rows);
		                    if (rows.length > 0) {
		                    	rp_ajax("rp/system/rescMng/deleteResources", {data: json}, function(text){
		                    		//grid.removeRows(rows, false);
		                    		grid.reload();
		                        });
		                    }
		                }
		            }
		        );
			}
		}
		
		function showAllResc(){
			moduleTree.selectNode(null);
			selectedModuleId = null;
			grid.load({});
		}
		
		function onNameFilterEnter(e) {
            var namebox = mini.get("nameFilter");
            var name = namebox.getValue().toLowerCase();
            if(selectedModuleId){
            	grid.load({moduleId: selectedModuleId, name:name});
            }else{
            	grid.load({name:name});
            }
        }
		
		function rebuildRescWarehouse(){
			mini.confirm("确定重建资源库吗？", "友情提示",
	            function (action) {
	                if (action == "ok") {
	                	var msgid = mini.loading("资源库正在重建，请稍后......");
	        			rp_ajax("rp/system/rescMng/rebuildRescWarehouse", {}, function(text){
	        				if(text == "success"){
	        					mini.alert("资源库重建成功");
	        					mini.hideMessageBox(msgid);
	        				}else{
	        					mini.alert("资源库重建失败");
	        				}
	        			});
	                }
	            }
	        );
			
		}
		
	</script>
  </body>
</html>
